home *** CD-ROM | disk | FTP | other *** search
/ Commodore Free 9 / Commodore_Free_Issue_09_2007_Commodore_Computer_Club.d64 / t.hexfiles 4 < prev    next >
File List  |  2023-02-26  |  9KB  |  332 lines

  1. u
  2.          The HEXFILES part 4
  3.             By Jason Kelk
  4.  
  5. Ere mister, wanna learn machine code?
  6. Well you've come to the right place
  7. ain't ya? Yup, welcome to another
  8. installment of the Hex Files and, as
  9. is becoming fairly common ground now,
  10. I'll start off by having a look at the
  11. solution to the "challenge" (since
  12. some people have said they are too
  13. easy) set in the previous
  14. installment.If you remember, we had a
  15. simple scroll routine and I asked you
  16. to move it down a line and change
  17. where the data was coming from to
  18. $e460.
  19.  
  20. Here's the modified routine:
  21.  
  22.   * = $0900
  23.   ldy #$00
  24. main  ldx #$00
  25.   lda #$fe
  26. raster  cmp $d012
  27.   bne raster
  28. moveDloopilda $0429,x
  29.   sta $0428,x  ; read/write moved from
  30. $0400/1
  31.   inx
  32.   cpx #$27
  33.   bne moveDloop
  34.   lda $e460,y  ; reads the new "text"
  35. from $e460
  36.   sta $044f  ; and this used to be
  37. $0427
  38.   iny
  39.   jmp main
  40.  
  41. Right, at this point you're all saying
  42. something along the lines of "that's
  43. all very well, but what good is it?"
  44. On it's own not an awful lot, but if
  45. you start adding other things to it
  46. you get a little demo and that's just
  47. what we are going to do. Now I expect
  48. you are all wondering how I expect you
  49. to be able to understand the code for
  50. a demo, right?
  51.  
  52. Well, it's not going to be a complex
  53. demo, just something simple with
  54. sprites, a piece of music and a
  55. slightly more complex scrolling
  56. message than the one above. And you
  57. don't even have to type the code in,
  58. it's all available to download as we
  59. go if you feel lazy! So first,
  60. download this installment's sample
  61. data, extract everything to the same
  62. directory and drop demoD1.asm into a
  63. passing text editor so we can have a
  64. prod around.
  65.  
  66. First off, since this is such a large
  67. piece of source I'll cover it in
  68. chunks and any commands we haven't
  69. covered already will be explained on
  70. the way past. The first part of the
  71. source is responsible for
  72. initialization, the setting up of the
  73. computer.
  74.  
  75. scrlcounti= $0340
  76. messcounti= $0341
  77. sineposxi= $0342
  78. sineposyi= $0343
  79.  
  80. sineDcurvei= $0e00
  81. sineDcurveD2i= $0f00
  82.  
  83. The above assigns the names scrlcount,
  84. messcount, sineposx and sineposy to
  85. various locations in memory and
  86. specify where to look for the two sine
  87.  curves  that are about to be loaded
  88. in. Apart from the curves, these are
  89. our counters  for various pieces of
  90. the code and the memory they have been
  91. given is part of  the cassette buffer
  92. so will cause no problems.
  93.  
  94.   .incbin sprites.prg
  95.   .incbin sines.prg
  96.   .incbin music.prg
  97.  
  98. These three are "INClude BINary" file
  99. commands; they're not 6510 machine
  100. code instructions but assembler
  101. directives, in other words they tell
  102. C64Asm to load those binary files and
  103. include them in the final PRG it
  104. makes. In this case, they're the
  105. sprite definitions (at $0c00 to
  106. $0dff), the sine curves we'll use to
  107. make the sprites move (at $0e00 to
  108. $0fff) and finally a piece of music by
  109. Sean of Cosine (at $1000 to $1xxxx).
  110.  
  111.   *= $0900
  112.  
  113.   sei
  114.  
  115. As usual the * command tells the
  116. assembler where we want our code put,
  117. in this case at $0900. And next we
  118. have a new 6510 command, SEI means SEt
  119. Interrupt disable status and basically
  120. it turns off the C64's interrupts
  121. which would otherwise interfere with
  122. the running of our code. You may have
  123. noticed the occasional jump in the old
  124. scroller as the system interrupts
  125. occurred at the same point on the
  126. screen as the routine and we don't
  127. want that. The actual use of SEI (for
  128. setting up interrupts) will be covered
  129. later on.
  130.  
  131.   ldx #$00
  132. clrpageiilda #$20
  133.   sta $0400,x
  134.   sta $0500,x
  135.   sta $0600,x
  136.   sta $06e8,x
  137.   lda #$0f
  138.   sta $d800,x
  139.   sta $d900,x
  140.   sta $da00,x
  141.   sta $dae8,x
  142.   inx
  143.   bne clrpage
  144.  
  145. Okay, this is one of those loop
  146. thingies isn't it? Yes, just like the
  147. loops we have seen before it's just
  148. putting a repeated character onto the
  149. screen. It's a bit different in that
  150. it does the whole screen and the
  151. colour memory at $D800 as well. You
  152. may be wondering about the last
  153. location being $06E8, well this is a
  154. little trick; the screen memory is
  155. 1,000 bytes long and runs from $0400
  156. to $07E7 and the maximum value a
  157. register can hold is $FF so $06E8 +
  158. $ff is $07E7. It saves having to do a
  159. shorter loop for the last bit of the
  160. screen. The value $20 is the space
  161. character so the whole screen is
  162. cleared and the value $0f is going to
  163. the colour memory to set all the
  164. characters light grey.
  165.  
  166.   lda #$00
  167.   sta $d020
  168.   sta $d021
  169.   sta scrlcount
  170.   sta messcount
  171.   sta sineposx
  172.   lda #$40
  173.   sta sineposy
  174.  
  175. This next piece of code puts 0 into
  176. the screen and border colours, making
  177. them black and also zeroes off our
  178. counters - at least, all except
  179. sineposy which we want to be different
  180. for the sine effect to work so it has
  181. a value of $40 instead.
  182.  
  183.   lda #$ff
  184.   sta $d015
  185.   sta $d01c
  186.   sta $d01d
  187.  
  188. Right, I've assumed that you all have
  189. some knowledge of sprites from the
  190. C64's manual but we'll clarify a
  191. little to be sure; the above four
  192. lines will, in order, turn on all
  193. eight sprites ($d015), make them
  194. multicolour ($d01c) and also alter
  195. their priority to put them under any
  196. characters on the screen like our
  197. scroller ($d01d). The eight bits in
  198. each of these locations represent one
  199. sprite, so the first sprite is the
  200. lowest bit and the last is the
  201. highest; since a value of #$ff has all
  202. of it's bits set that means all eight
  203. sprites are being turned on and then
  204. all have their multicolour enabled and
  205. are set to "low" priority.
  206.  
  207.   ldx #$00
  208.   lda #$30
  209. setsdpiista $07f8,x
  210.   clc
  211.   adc #$01
  212.   inx
  213.   cpx #$08
  214.   bne setsdp
  215.  
  216. Next we set the sprite data pointers
  217. (S.D.P.'s for short) using another
  218. loop. But this time there are two
  219. commands we haven't covered again.
  220. These are CLC (CLear Carry flag) and
  221. ADC (Add Decimal Accumulator). The
  222. former's job is to empty the carry
  223. flag, which is one of a series of
  224. flags the C64 uses to remember things
  225. (in this case, it's an "overflow" for
  226. various calculations). The reason for
  227. it's use here will not be immediately
  228. apparent until I explain the ADC
  229. command that follows it. ADC #$01 will
  230. add one to whatever value is in the
  231. accumulator, so if it's presently $30
  232. as at the start of the loop the ADC
  233. command will make it $31. It's like
  234. the INX command except that by
  235. changing the value you can alter how
  236. much the A changes by and, if the
  237. carry flag is set from any previous
  238. job, then it will be added in too!
  239. Since we don't want this to happen
  240. here because the carry will only have
  241. been set by something we don't want to
  242. affect this particular ob, we use the
  243. CLC first. But what does this loop
  244. actually do? Well, it puts a value of
  245. $30 into $07F8, a value of $31 into
  246. $07F9 and soon until it puts $37 into
  247. $07Ff, all of which point the VIC to
  248. $0C00, $0C40 and so on to $0dC0 for
  249. the sprite data. If you look at those
  250. sprites in memory they spell H E X F I
  251. L E S (we have put in two E
  252. definitions to make this a bit easier
  253. for now).
  254.  
  255.   lda #$0b
  256.   sta $d025
  257.   lda #$0f
  258.   sta $d026
  259.   ldx #$00
  260.   lda #$0e
  261. spriteciista $d027,x
  262.   inx
  263.   cpx #$08
  264.   bne spritec
  265.  
  266. Now we have to set the sprite colours.
  267. $D027 onwards govern the sprite
  268. colours and the value of $0e will
  269. produce the same light blue as the
  270. border colour when the C64 is turned
  271. on. The multicolours are set to dark
  272. grey and light grey so the sprites can
  273. have a nice shading effect.
  274.  
  275.   lda #$00
  276.   jsr $1000
  277.  
  278. The final part of the setup we will
  279. cover this issue is another new
  280. command. JSR means Jump to Sub Routine
  281. and it's the machine code equivalent
  282. of the BASIC command GOSUB. Up until
  283. now we have used the RTS command to
  284. drop back to BASIC after our code has
  285. finished, but if we were to JSR to a
  286. piece of code an RTS would return us
  287. back to the next command along so it
  288. allows us to pop off temporarily and
  289. do something else, then come back and
  290. continue where we left off. In this
  291. case it calls the part of the music
  292. routine (it was included earlier with
  293. the INCBIN command, remember?) that
  294. sets the music up for playing and
  295. turns the volume on. The LDA #$00 is
  296. to tell the music routine that we want
  297. tune 0, the first one it has and later
  298. on there's a JSR $1003 that actually
  299. plays the music.
  300.  
  301. Right, that's yer lot for another
  302. time, I hope you're all following
  303. okay. If you want to, you can try to
  304. carry on alone and examining the rest
  305. of the program, although there are
  306. some new commands I will cover in the
  307. next installment. And your mission,
  308. should you decide to accept it, is to
  309. try and change the sprite colours to
  310. $04 (for purple) and the message
  311. colour to $01 (for white). Good luck
  312. and, as usual, you can email me if you
  313. have any questions on this
  314. installment. This web site will self
  315. destruct in ten seconds.
  316.  
  317. The source code for the routines above
  318. can be downloaded here
  319.  
  320. http://www.oldschool-gaming.com
  321. /files/c64/hexDfiles/partD4Dfiles.zip
  322.  
  323. for easier reference.
  324.  
  325. (c) Jason Kelk
  326. RE-printed with the permission of the
  327. copyright holder,
  328.  
  329.  
  330. ...end...
  331. www.commodorefree.com
  332.  
  333.